package com.mobiusws.reports.action;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.mobiusws.reports.dao.DailyFraudSummaryDao;
import com.mobiusws.reports.dao.TestDao;
import com.mobiusws.reports.dao.WeeklyFraudSummaryDao;
import com.mobiusws.reports.model.DailyFraudSummary;
import com.mobiusws.reports.model.LoginInfo;
import com.mobiusws.util.DateUtil;
import com.mobiusws.util.ExportManage;
import com.mobiusws.util.ExportReport;
import com.mobiusws.util.JsonUtil;
import com.mobiusws.util.MaskUtil;
import com.mobiusws.util.MessageUtil;
import com.mobiusws.util.OperatorConstants;
import com.opensymphony.xwork2.ActionSupport;

public class WeeklyFraudSummaryAction extends ActionSupport {

	private static final long serialVersionUID = 5395964004991248282L;

	@Resource
	private WeeklyFraudSummaryDao weeklyFraudSummaryDao;
	@Resource
	private JsonUtil jsonUtil;
	@Resource
	private MessageUtil messageUtil;
	private String result;

	private String fileName;
	private String bQuery;
	private String suffix;
	private String startDate;
	private String endDate;
	private int start;
	private int limit;
	private int total;

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getBQuery() {
		return bQuery;
	}

	public void setBQuery(String bQuery) {
		this.bQuery = bQuery;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	// a getter is required to get the result in pages
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
	
	private void setJsonResult() {
		List<DailyFraudSummary> list = weeklyFraudSummaryDao
				.getWeeklyFraudSummary(this);
		this.result = jsonUtil.getJsonByList(list, this.getTotal());
	}

	@Override
	public String execute() throws Exception {
		// Thread.sleep(1000);
//		System.out.printf("selDate=%s, start=%s, limit=%s\n",
//				this.getSelDate(), this.getStart(), this.getLimit());
		this.setJsonResult();
		// System.out.println(this.result);
		ServletActionContext.getResponse().setContentType(
				"text/plain;charset=utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(this.result);
		out.flush();
		out.close();
		return SUCCESS;
	}
	
	public String export() throws Exception {
		if(isMaxExceeded(total)) {
			this.bQuery="E";
		} else {
			this.bQuery="C";
			this.fileName = createExportFiles()+"."+this.suffix;
		}
		return "export";
	}
	
	private boolean isMaxExceeded(int total) {
		String maxSizeStr=messageUtil.getString("max.out.size");
		int maxSize=Integer.parseInt(maxSizeStr);
		return total>maxSize;
	}
	
	private String createExportFiles() {
		  ExportManage exportManage = new ExportManage();
		  ExportReport reportStruct = new ExportReport();
		  //HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		  
		//  System.out.println("user:"+(String) session.getAttribute("SPRING_SECURITY_LAST_USERNAME"));
		  reportStruct.setReportName("weekly_fraud_summary_report");
		  reportStruct.setReportDate(this.getStartDate().replaceAll("-", ""));
		  reportStruct.setReportTitle("Traffic Date,Source,MSISDN,IMSI,Cell ID,LAC,IMEI,Vendor,Model,H/M,Illegal Odds 7 days,Illegal Odds 24 hrs,Calls Out,B Count,B Ratio,Num Out Adjusted,B2 Ratio,Avg Duration,Off Peak,Bursting,Cells,Calls In,SMS Out,SMS In,CLIR,Audit,Verify,Seconds,Units,Possible Loss");
		  reportStruct.setQueryCriteria(this.getStartDate()+"  -  "+this.getEndDate());
		  //System.out.println(this.getStartDate()+"  -  "+this.getEndDate());
		  String[] tableColumnsArray = {"traffic_date","source","msisdn","imsi","cell_id","lac","imei","vendor","model","handset_or_module","illegal_odds_7_days","illegal_odds_24_hours","calls_out","b_count","b_ratio","b2_out","b2_ratio","avg_dur","off_peak","bursting","cells","calls_in","sms_out","sms_in","clir","audit","verify","seconds","units","possible_loss"};
		  reportStruct.setTableColumnsArray(tableColumnsArray);
		  String[] reportTitleArray = {"Traffic Date","Source","MSISDN","IMSI","Cell ID","LAC","IMEI","Vendor","Model","H/M","Illegal Odds 7 days","Illegal Odds 24 hrs","Calls Out","B Count","B Ratio","Num Out Adjusted","B2 Ratio","Avg Duration","Off Peak","Bursting","Cells","Calls In","SMS Out","SMS In","CLIR","Audit","Verify","Seconds","Units","Possible Loss"};
		  reportStruct.setReportTitleArray(reportTitleArray);
		  reportStruct.setTableName("daily_fraud_summary_report");
		  
		  long maskBits = OperatorConstants.MASK_VALUE;
		  String outputPath = "/opt/GK2/portal/querydata";
//		  String outputPath = "E:/TestFiles/export";
		  
		  exportManage.DBQueryReportDaily(OperatorConstants.MCC+"_"+OperatorConstants.MNC, "0", maskBits, reportStruct, outputPath, weeklyFraudSummaryDao.getQueryList(this));
		  //  System.out.println(reportStruct.getReportName()+"_"+reportStruct.getReportDate());
		  return reportStruct.getReportName()+"_"+reportStruct.getReportDate();
		 }

}
